<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加批量订单</title>
<script language="javascript">
    $(document).ready(function(){
        $("#saveForm").click(function () {
            var param = $('form').serialize();
            $.post("/batchorder/addBatchOrder",{param: param}, function (data) {
                if (data.code) {
                    alert(data.msg);
                    location.href = '/batchorder/batchOrderList'
                } else {
                    alert(data.msg);
                    return false;
                }
            }, "json");
        });
    });
    //插入相关数据
    function insert_row(){
        var measure = document.getElementById("measure").value;
        var type = document.getElementById("type").value;
        var gender = document.getElementById("gender").value;
        if(measure=='0'){
            alert("请选择尺寸");
            return;
        }
        if(type=='0'){
            alert("请选择版型");
            return;
        }
        if(gender=='0'){
            alert("请选择性别");
            return;
        }
        //获取裁剪人员与车工人员
        var factory_id = $('#factorys').val();
        $.post("/batchorder/searchEmployee",{factory_id:factory_id,},function(data){
            var workers = data.workers;
            var caijian = data.caijian;
            //车工人员
            var str_workers = "<select class='select1' id='workers' name='workers[]'>";
            str_workers += "<option value=''>--------车工人员----------</option>";
            for (var i=0;i<workers.length;i++)
            {
                str_workers += "<option value='"+workers[i].id+"'>"+workers[i].name+"</option>";
                //alert(workers[i].name);return ;
            }
            str_workers += "</select>";
            //裁剪人员
            var str_caijian = "<select class='select1' id='caijian' name='caijian[]'>";
            str_caijian += "<option value=''>--------裁剪人员----------</option>";
            for (var i=0;i<caijian.length;i++)
            {
                //str_caijian += "<option value='caijian[i].id'>caijian[i].name</option>";
                str_caijian += "<option value='"+caijian[i].id+"'>"+caijian[i].name+"</option>";
            }
            str_caijian += "</select>";
            //alert(str_workers);return ;

            //模拟拼接的表行
            var trlenth = $("#showtable tr").length; //获取表的行数
            var table=document.getElementById("showtable");
            var row=table.insertRow(trlenth-1);
            var cell1=row.insertCell(0);
            var cell2=row.insertCell(1);
            var cell3=row.insertCell(2);
            var cell4=row.insertCell(3);
            //注释修改后不用的
            //var cell3=row.insertCell(2);
            //var cell4=row.insertCell(3);
            //var cell5=row.insertCell(4);
            var set_name = "goods-"+factory_id+"[]";
            var set_input = "<input type='text' id='"+set_name+"' name='protypename[]' value='"+measure+","+type+","+gender+"' readonly='readonly' />";
            cell1.innerHTML=set_input;
            cell2.innerHTML="<input type ='text' name='ordernum[]'  onblur='checkDecimal(this)' placeholder='请输入生产数量'/>";
            cell3.innerHTML="<input type ='text' name='beizhu[]'   placeholder='请输入备注信息'/>";
            cell4.innerHTML="<a href='javascript:void(0);' onclick='deleteRow(this)'>删除</a>";
            //cell3.innerHTML=str_caijian;
            //cell4.innerHTML=str_workers;
           // cell5.innerHTML="<a href='javascript:void(0);' onclick='deleteRow(this)'>删除</a>";
        },"json");
    }
    function deleteRow(obj){
        if(confirm('你确定删除吗?')){
            var table=document.getElementById("showtable");
            table.deleteRow(obj.parentElement.parentElement.rowIndex);
        }
    }
    function checkDecimal(obj) {
        //var decimalReg=/^\d{0,9}\)?$/; //只能输入数字
        var decimalReg = /^\d+(?=\.{0,1}\d+$|$)/; //可以输入数字或小数点
        if(obj.value!=""&&decimalReg.test(obj.value)){
        }else {
            if (obj.value != "") {
               //alert('非法值！请重新输入！');return ;
                obj.value = 0
            }
        }
    }
    function changeFactory(){
        location.reload();
    }
    function getSku()
    {
        var sku = $("#goods_sku").val();
        if(sku){
            //搜索该商品编号对应的价格信息
            $.post("/batchorder/ajaxbutton?op=searchProductPrice",{goods_id: sku},function(data){
                if(data.code)
                {
					$("#skuerr").html('');
                    if(data.countlistsku==0)
					{
						//没有建立五位的关联SKU
						alert("暂时没有建立关联SKU，请先关联！");
						$("#skuerr").html("<a href=/product/addParentProduct?pid="+data.goods_parentID+"><font color=red>点击此处建立关联SKU</font></a>");
						return;	
					}
					$('#price').val(data.msg);
					$('#factorys').val(data.g_supplier);
					$("#hidden_data").val(JSON.stringify(data));	//json转字符串
					datalist = eval(data.listsku);
						var rs_str = '<table border="0" id="search_rs" cellpadding="5px"><tr><td colspan="9"><a href="#">[关闭-搜索结果]需要的打勾</a></td></tr><tr class="thead"><td>&nbsp;</td><td width=120>编号</td><td >商品编号</td><td >商品名</td><td >尺码</td></tr>';
								for(var i=0;i<datalist.length;i++){
									rs_str += '<tr><td><input type="checkbox" value="'+i+'"></td><td>'+datalist[i].goods_id+'</td><td>'+datalist[i].goods_sku+'</td><td>'+datalist[i].goods_name+'</td><td>'+datalist[i].goodstypelist+'</td></td>';
								}
								rs_str += '<tr><td colspan="9"><a href="#">[关闭-搜索结果]</a></td></tr></table>';
								$(document.body).append(rs_str);
								$("#search_rs").css("left","400px");
								$("#search_rs").css("top","270px");
								$("#search_rs a").click(function(){
									$("#search_rs").remove();
								});
								$("#search_rs input[type='checkbox']").click(function(){
									if(this.checked){
										var newdata = jQuery.parseJSON($("#hidden_data").val());
										var datadis = newdata.listsku;
										var datas = datadis[this.value];
										/*if(datas.goodstypelist==null)
										{
											alert("尺码信息不能用为空！请联系商品维护人员设置尺码信息!");
											return;
										}*/
										
										$("#purchaselist").show();
										$("#purchaselist").append('<tr id="order_list_tr_'+this.value+'"><td bgcolor="#FFFFFF" class="pro_data_col_cos"><img src=/fuman/images/del.png class=p_del  style="cursor:pointer" title="删除" onclick="delatt(this)"><span id="isnull_'+datas.goods_id+'" style=display:none>'+datas.goods_id+'</span><input type="hidden" readonly="true" size="20" name="attID[]" value="'+datas.goods_id+'" /><input type="hidden" readonly="true" size="20" name="attsku[]" value="'+datas.goods_sku+'" /></td><td bgcolor="#FFFFFF" class="pro_data_col_cos">'+datas.goods_sku+'</td><td bgcolor="#FFFFFF" class="pro_data_col_cos">'+datas.goods_name+'</td><td bgcolor="#FFFFFF" class="pro_data_col_cos">'+'<input type="text" size="20" name="protypename[]" value="'+datas.goodstypelist+'"  placeholder=\'请输入型号，格式：M,亚洲,男\' /></td><td bgcolor="#FFFFFF" class="pro_data_col_cos"><input type =\'text\' name=\'ordernum[]\'  onblur=\'checkDecimal(this)\' placeholder=\'请输入生产数量\'/></td><td bgcolor="#FFFFFF"><input type =\'text\' name=\'beizhu[]\'   placeholder=\'请输入备注信息\'/></td></tr>');
									}else{
										$("#purchaselist #order_list_tr_"+this.value).remove();
									}

					});

					
					
                   
					


                }else{
                    //alert(data.code);return false;
                    $("#skuerr").html(data.msg).css('color','red');
                    $('#price').val('');
                }
            },"json");
        }else{
            $("#skuerr").html('没有输入任何数据!').css('color','red');
        }
    }
function delatt(o)
{
	$(o.parentNode.parentNode).remove();
}
//确认保存相关操作
function save() {
   if($("#finish_day").val()){
       var finish_day = $("#finish_day").val();
   }else{
       alert("期望交货日期不能为空");return ;
   }
    if($("#goods_sku").val()){
        var goods_sku = $("#goods_sku").val();
    }else{
        alert("商品编号不能为空");return ;
    }
    if($("#price").val()){
        var price = $("#price").val();
    }else{
        alert("价格不能为空");return ;
    }
    if($("#factorys").val()){
        var factorys = $("#factorys").val();
    }else{
        alert("工厂必须选择");return ;
    }
    //ordernum 订单数量
    var obj =    document.getElementsByName('ordernum[]');
    if(obj.length){
        for (var i=0;i<obj.length;i++)
        {
            if(obj[i].value){
            }else{
                alert("生产数量不能为空!");return false;
            }
        }

    }else{
        alert("没有任何订单,请添加订单!");return false;
    }
    //protypename 型号判断
    var protype =    document.getElementsByName('protypename[]');
    if(protype.length){
        for (var i=0;i<protype.length;i++)
        {
            if(protype[i].value){
            }else{
                alert("型号不能为空！");return false;
            }
        }

    }
    $("#otherForm").submit();
}
</script>
<body class="withvernav">
<div class="bodywrapper"> {{include file='/public/leftmenu.tpl'}}
    <div class="centercontent">
        <div class="pageheader notab">
            <h1 class="pagetitle">添加批量订单</h1>
        </div>
        <!--pageheader-->
        <div id="contentwrapper" class="contentwrapper">
            <div id="basicform" class="subcontent">
                <form class="stdform stdform2"  id="otherForm" method="post" action="/batchorder/addBatchOrder" enctype="multipart/form-data" >
                 <input type="hidden" id="hidden_data" value="" />

                      <table cellpadding="0" cellspacing="0" border="0" class="stdtable">
            <thead>
              <tr>
                <th colspan="2" class="head1">添加订单</th>
              </tr>
            </thead>
          </table>
                      <table cellpadding="0" cellspacing="0" border="0" class="stdtable" style="table-layout: fixed;">
                        <tbody>
                        <tr>
                            <td class="head1">期望交货日期</td>
                            <td class="head0" colspan="4"><input type="text" id = "finish_day" name="finish_day" class="width100" value="{{$goods.finish_day}}" onClick="WdatePicker({dateFmt:'yyyy-MM-dd '})" class="width100 hasDatepicker"></td>
                          
                        </tr>
                        <tr>
                            <td class="head1">商品编号</td>
                            <td class="head0"  colspan="4">
                                <input type="text" name="goods_sku" onBlur="getSku(this)" id="goods_sku" class="width400" value="{{$goods.goods_sku}}">
                                <br/>
                                <span id="skuerr"></span>
                            </td>
                           

                        </tr>
                        <tr>
                            <td class="head1">工价</td>
                            <td class="head0"  colspan="4"><input type="text" onblur='checkDecimal(this)' id= "price" name="price" class="width100" value="{{$goods.print_price}}"></td>
                        </tr>
                          <tr>
                            <td class="head1">所属工厂</td>
                            <td colspan="4" class="head0">
                                <select class="selectCss2" name="factorys" id="factorys" >
                                    <option value="">请选择</option>
                                    {{foreach from=$factorys item=res key=key}}
                                        <option value="{{$res.id}}"> {{$res.name}} </option>
                                    {{/foreach}}
                                </select></td>
                            </td>
                        </tr>
                       
                     <!--
                       <tr>
                            <td class="head1">订单属性</td>
                            <td colspan="4" class="head0">
                                <input name="property" type="radio" value="1" checked/>批量
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input name="property" type="radio" value="2" />关联
                            </td>
                        </tr>
                        <tr>
                            <td class="head1">尺寸</td>
                            <td colspan="4" class="head0">
                                <select class="selectCss2" name="measure" id="measure">
                                    <option value="0">请选择</option>
                                    <option value="L">L</option>
                                    <option value="M">M</option>
                                    <option value="S">S</option>
                                    <option value="XL">XL</option>
                                    <option value="XS">XS</option>
                                    <option value="XXL">XXL</option>
                                    <option value="XXS">XXS</option>
                                    <option value="XXXL">XXXL</option>
                                    <option value="XXXS">XXXS</option>
                                    <option value="XXXXL">XXXXL</option>
                                </select></td>
                            </td>
                        </tr>
                        <tr>
                            <td class="head1">版型</td>
                            <td colspan="4" class="head0">
                                <select class="selectCss2" name="type" id="type">
                                    <option value="0">请选择</option>
                                    <option value="亚洲"> 亚洲 </option>
                                    <option value="亚码"> 亚码 </option>
                                    <option value="欧美"> 欧美 </option>
                                    <option value="通用"> 通用 </option>
                                </select></td>
                            </td>
                        </tr>
                        <tr>
                            <td class="head1">性别</td>
                            <td colspan="4" class="head0">
                                <select class="selectCss2" name="gender" id="gender">
                                    <option value="0">请选择</option>
                                    <option value="男"> 男 </option>
                                    <option value="女"> 女 </option>
                                    <option value="通用"> 通用 </option>
                                </select></td>
                            </td>
                        </tr>
                       
                        <tr>
                            <td class="head1">
                                <button type="button" id ="insertForm" class="btn btn-primary" onClick="insert_row();"  >插入</button>
                            </td>
                            <td colspan="4" class="head0">
                            </td>
                        </tr>-->
                        <tr>
                            <td class="head1" colspan="5">
                               <table width="100%" border="0" id="purchaselist" class="table" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC" style="display:none">
	<thead>
    <tr>
      <th class="pro_data_col_cos">操作</th>
      <th class="pro_data_col_cos">编号</th>
      <th class="pro_data_col_cos">商品名</th>
	  <th class="pro_data_col_cos">型号</th>
      <th class="pro_data_col_cos">生产数量</th>
      <th class="pro_data_col_cos">生产备注</th>
	 
    </tr>
	
  </thead>
  </table>
                            </td>
                           
                        </tr>
                        <tr id="beizhu">
                            <td class="head1">订单备注</td>
                            <td class="head0" colspan="4">
                                <textarea name="memo" cols="10" rows="6"></textarea>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                 

                    <table cellpadding="0" cellspacing="0" border="0" class="stdtable">
                        <thead>
                        <tr>
                            <td><button type="button" class="btn btn-primary" onClick="save()">确认保存</button>
                            </td>
                        </tr>
                        </thead>
                    </table>
                </form>
                <br />
            </div>
            <!--subcontent-->

            <!--subcontent-->

        </div>
        <!--contentwrapper-->

    </div>
    <!-- centercontent -->

</div>
<!--bodywrapper-->

</body>
</html>